class TalksController < ApplicationController
  before_filter :load_user
  
  # GET /talks
  # GET /talks.xml
  def index
    

    @user_talks = @user.user_talks.sort_by{|user_talk| [user_talk.star, user_talk.talk.latest_update_at(user_talk.talk)]}.reverse
    #@user_talks = @user.user_talks.sort_by{|user_talk| user_talk.star}
    #@user_talks0 = @user.user_talks.sort_by{|user_talk| user_talk.talk.latest_update_at(user_talk.talk)}.reverse
    #@user_talks1 = @user_talks0.find_all(:star => true)
    #@user_talks << @user_talks1
    #@user_talks2 = @user_talks0.find(:all, :star => false)
    #@user_talks << @user_talks2
    #@talks = Talk.all
    #@talks = @user.talks.sort_by{|talk| [talk.star(talk,@user), talk.latest_update_at(talk)]}.reverse
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @talks }
    end
  end
  
  # GET /talks/1
  # GET /talks/1.xml
  def show
    #@user = User.find(session[:user_id])
    @talk = Talk.find(params[:id])
    @user_talk = UserTalk.find(:first, :conditions => [ "user_id = ? AND talk_id = ?", @user.id, @talk.id ])
    @talkees = @talk.talkees
    session[:talk_id] = params[:id] if params[:id]
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @talk }
    end
  end
  
  def show_this_talker_only
    @user = User.find(session[:user_id])
    @talker = User.find(params[:id])
    @talk = Talk.find(session[:talk_id])
    @user_talk = UserTalk.find(:first, :conditions => [ "user_id = ? AND talk_id = ?", @talker.id, @talk.id ])
    @talkees = @talk.talkees.find(:all, :conditions => [ "user_id = ?", @talker.id])

    respond_to do |format|
      format.html { render :action => "show" }
      format.xml  { render :xml => @talk }
    end
  end
  # GET /talks/new
  # GET /talks/new.xml
  def new_talk
    @talk = Talk.new(params[:talk])
    flash[:notice] = "talk created"
    @talk.created_by = @user.id
    @talk.save

    @talk.users << @user

    @user_talk = UserTalk.find(:first, :conditions => [ "user_id = ? AND talk_id = ?", @user.id, @talk.id ])

  end
  
  # GET /talks/1/edit
  def edit
    @talk = Talk.find(params[:id])
  end
  
  # POST /talks
  # POST /talks.xml

  
  def new_talkee
    @talk = Talk.find(session[:talk_id])
    @new_talkee = @talk.talkees.build(params[:talkee])

    @new_talkee.user = @user
    @new_talkee.save
  end
  # PUT /talks/1
  # PUT /talks/1.xml
  def update
    @talk = Talk.find(params[:id])
    
    respond_to do |format|
      if @talk.update_attributes(params[:talk])
        flash[:notice] = 'Talk was successfully updated.'
        format.html { redirect_to(@talk) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @talk.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /talks/1
  # DELETE /talks/1.xml
  def destroy
    @talk = Talk.find(params[:id])
    @talk.destroy
    
    respond_to do |format|
      format.html { redirect_to(talks_url) }
      format.xml  { head :ok }
    end
  end
  
  def change_star
    #@talk = Talk.find(params[:id])
    @user_talk = UserTalk.find(params[:id])

    @user_talk.toggle!(:star)
    respond_to do |format|
        flash[:notice] = 'Talk was successfully updated.'
        format.html { redirect_to(talks_url) }
        format.xml  { head :ok }
    end
  end
  
  def change_star_show
    #@talk = Talk.find(params[:id])
    @user_talk = UserTalk.find(params[:id])

    @user_talk.toggle!(:star)
    respond_to do |format|
        flash[:notice] = 'Talk was successfully updated.'
        format.html { redirect_to(talk_url(session[:talk_id])) }
        format.xml  { head :ok }
    end
  end
    
  def load_user
    if params[:user_id] 
      session[:user_id] = params[:user_id] 
    end
    @user = User.find(session[:user_id])
  end
  
  def reply_to_talkee
    @talk = Talk.find(session[:talk_id])
    @talkee_master = Talkee.find(params[:id])
    respond_to do |format|
        format.html { render :controller => "talkee", :action => "new", :id => @talk }
        format.xml  { head :ok }
    end
  end
end
